package com.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;

public class DateUtil {

	public static String getChar6() {
		return DateFormatUtils.format(new Date(), "yyyyMM");
	}

	public static String getChar8() {
		return DateFormatUtils.format(new Date(), "yyyyMMdd");
	}

	public static String getChar12() {
		// 格式化日期时间
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
	}

	public static String getChar14() {
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
	}

	public static String getTimeChar6() {
		return DateFormatUtils.format(new Date(), "HHmmss");
	}

	public static String getTimeChar10() {
		return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	public static String getChar10() {
		return DateFormatUtils.format(new Date(), "yyyy-MM-dd");
	}

	public static String formateDate(Date date, String format) {
		if (null == format || "".equals(format)) {
			format = "yyyyMMdd";
		}

		if (null == date) {
			date = new Date();
		}

		return DateFormatUtils.format(date, format);
	}

	public static Date formatStr2Date(String date, String format) {
		if (null == format || "".equals(format)) {
			format = "yyyy-MM-dd";
		}

		if (null == date) {
			return new Date();
		}

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			return new Date();
		}
	}

}
